<?php
namespace app\appletsapi\controller;

use app\common\model\Cart as CartModel;
use app\common\model\Order as OrderModel;
use app\common\model\Payments;
use app\appletsapi\validate\Place as PlaceValidate;
use app\common\model\ShopsSeat;
use think\facade\Config;

class Place extends Base
{
//    /**
//     * @route('applets/GoToPlace')
//     * 购物车下单
//     */
    public function GoToPlace()
    {
        $param = input('post.');
        $validate = new PlaceValidate();
        $res = $validate->scene('place')->check($param);
        if(!$res){
            $this->result('',-1,$validate->getError());
        }
        $CartModel = new CartModel();
        $OrderModel = new OrderModel();
        $user = $this->GetUserInfo();
//        var_dump($user);die;
        $data = $CartModel->Settlement($user,$param['cart_ids'],'',2);
//		var_dump($data);die;
        if(!$data){
            $this->result([],-1,$CartModel->getError());
        }
        $data['receive_time'] = $param['receive_time'];
        $data['receive_name'] = $param['receive_name'];
        $data['r_phone'] = $param['r_phone'];
        $data['r_address'] = $param['r_address'];
        $createorders = $OrderModel->placeofflineorder($user,$param['cart_ids'],$data);
//		    var_dump($createorders);die;
        if($createorders['code'] === false){
            $this->result([],-1,$OrderModel->getError());
        }

//		var_dump($createorders);
        $wxconfig = Config::pull('weixin_applet');
//		    var_dump($wxconfig);die;
        $notify = "/applets/notify";
        $rs = $OrderModel->wxpay($user,$createorders['data']['paymentsid'],$wxconfig,$notify);
        if($rs === false){
            $this->result([],-1,$OrderModel->getError());
        }
        $rs['data']['payments_id'] = $createorders['data']['paymentsid'];
        $rs['data']['order_id'] = $createorders['data']['orderid'];
        $rs['data']['is_pay'] = false;
//        var_dump($rs);die;
        $this->result($rs,1,'操作成功');
    }
    
    /**
     * @route('applets/ToBePaid')
     * 订单待支付去支付
     */
    public function ToBePaid()
    {
        $orderModel = new OrderModel();
        $user = $this->GetUserInfo();
//        var_dump($user);die;
        $where = [
//            'user_id' => $user['user_id'],
            'order_id' => input('post.order_id'),
        ];
        $thecancel = $orderModel->tobepaidordercheck($where);
//        var_dump($thecancel);die;
        $result['data']['is_can_pay'] = false;
        switch($thecancel['code']){
            case 2:
                $details = $thecancel['data']['details'];
                $details = array_filter($details,function($element){
                    //只保留数组中的库存id不为0的数组
                    return $element['p_s_id'] != 0;
                });
//                var_dump($details);die;
                $rs = $orderModel->updateorderandstock($details,$thecancel['data']['order']);
                if($rs === false){
                    $this->result($result,-1,$orderModel->getError());
                }
                $this->result($result,1,$thecancel['msg']."已取消订单");
                break;
            case 1:
                $paymentModel = new Payments();
                $orderno = "wx".date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
                $payment_rs = $paymentModel->where('payments_id','=',$thecancel['data']['order']['payments_id'])->update(['order_nos'=>$orderno]);
                if($payment_rs == false){
                    $this->result($result,-1,'更新购买记录单号失败');
                }
                $wxconfig = Config::pull('weixin');
//                var_dump($wxconfig);die;
                $notify = "/applets/notify";
                $rs = $orderModel->wxpay($user,$thecancel['data']['order']['payments_id'],$wxconfig,$notify);
                if($rs === false){
                    $this->result($result,-1,$orderModel->getError());
                }
                $result['data'] = $rs;
                $result['data']['payments_id'] = $thecancel['data']['order']['payments_id'];
                $result['data']['order_id'] = $thecancel['data']['order']['order_id'];
                $result['data']['is_can_pay'] = true;
                $this->result($result,1,'操作成功');
                break;
            default:
                $this->result($result,-1,$thecancel['msg']);
                break;
        }
    }
}
